<?php

class Crud_Form_Element_Column extends Crud_Form_Element
{
	/**
	 * Column for creating element.
	 * 
	 * @var Crud_Db_Table_Column_Abstract
	 */
	protected $_column;
	
	/**
	 * Create element by table column.
	 * 
	 * @param Crud_Db_Table_Column_Abstract $column
	 * @return void
	 */
	public function __construct($column)
	{
		if (!$column instanceof Crud_Db_Table_Column_Abstract) {
			throw new Crud_Exception('Cant create element by column. Unknow instance given');
		}
		
		$this->_column = $column;
		$name = $column->getName();
		
		// Create class and store in object
		$class = $this->_getElementClass();
		$element = new $class($name);
		$this->setElement($element);
		
		$element->setRequired(!$column->isNull());
		$element->setLabel($column->getLabel());
		
		// Adding trim filtering
		$element->addFilter(new Zend_Filter_StringTrim());
		
		$trim = new Zend_Filter_StringTrim();
		$null = new Zend_Filter_Null(array(
			Zend_Filter_Null::STRING,
			Zend_Filter_Null::EMPTY_ARRAY,
		));
		
		$element->addFilter(new Zend_Filter_Null(array(
			Zend_Filter_Null::STRING,
			Zend_Filter_Null::EMPTY_ARRAY,
		)));
		
		// Adding validator for length if length exists in metadata
		if ($maxLength = $column->getMaxLength()) {
			//$element->addValidator(new Zend_Validate_StringLength(0, $maxLength));
		}

		// Value greater than 0 where column is unsigned
		if ($column->isUnsigned()) {
			//$element->addValidator(new Zend_Validate_GreaterThan(0));
		}
		
/*		if ($column->isUnique()) {
			$table = $column->getTable()->getTableName();
			$name = $column->getName();
			$element->addValidator(new Zend_Validate_Db_NoRecordExists($table, $name));
		}*/
		
		// Add filters and validators specified for column.
		$element->addValidators($column->getValidators());
		$element->addFilters($column->getFilters());
		
		// Add specified validators and filters by type
		// @deprecated . Need to create class for each column type.
		$type = $column->getType();
		$this->init();
		
		return ;
		switch ($type) {
			case 'tinyint' :
				$element->addValidator(new Zend_Validate_Digits());
				$element->addValidator(new Zend_Validate_LessThan(127));
				break;
				
			case 'smallint' :
				$element->addValidator(new Zend_Validate_Digits());
				$element->addValidator(new Zend_Validate_LessThan(32767));
				break;
				
			case 'mediumint' :
				$element->addValidator(new Zend_Validate_Digits());
				$element->addValidator(new Zend_Validate_LessThan(8388607));
				break;
				
			case 'int' :
				$element->addValidator(new Zend_Validate_Digits());
				$element->addValidator(new Zend_Validate_LessThan(2147483647));
				break;
				
			case 'bigint' :
				$element->addValidator(new Zend_Validate_Digits());
				$element->addValidator(new Zend_Validate_LessThan(9223372036854775807));
				break;
				
			case 'float' :
			case 'double' :
				$element->addValidator(new Zend_Validate_Float());
				break;
				
/*			case 'date' :
				$element->addFilter(new Crud_Filter_Date());
				break;
				
			case 'datetime' :
				$element->addFilter(new Crud_Filter_Datetime());
				break;
				
			case 'time' :
				$element->addFilter(new Crud_Filter_Time());
				break;
				
			case 'timestamp' :
				$element->addFilter(new Filter_StringToTime());
				break;*/
				
			case 'char' :
				$element->addFilter(new Zend_Filter_StripNewlines());
				$element->addValidator(new Zend_Validate_Alpha());
				break;
				
			case 'varchar' :
				$element->addFilter(new Zend_Filter_StripNewlines());
				break;
				
			case 'enum' :
				//$element->addValidator(new Zend_Validate_InArray());
				break;
			default :
				break;
		}
		
		$this->init();
	}
	
	public function init() {}
	
	/**
	 * @return Crud_Db_Table_Column_Abstract
	 */
	public function getColumn()
	{
		return $this->_column;
	}
	
	/**
	 * Return element class name by column type.
	 * Try to get class from config or automatic from column type.
	 * 
	 * @return string
	 */
	protected function _getElementClass()
	{
		$column = $this->getColumn();
		
		$class = null;
		if ($config = $column->getConfig()) {
			$class = $config->class;
		}
		
		if (!$class) {
			$type = $column->getType();
			switch ($type) {
					case 'text'    :
						$suffix = 'Textarea';
						break;
					case 'blob'    :
						$suffix = 'File';
						break;
					case 'enum' :
						$suffix = 'Select';
						break;
					case 'int'     :
					case 'varchar' :
					default        :	
						$suffix = 'Text';
						break;
			}
			
			$class = 'Zend_Form_Element_' . $suffix;
		}
		
		return $class;
	}
}